package grafica.moderador.ventanas;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;

import logica.fachadas.IFachadaModerador;
import logica.fachadas.IObserver;

import grafica.moderador.controladores.ControladorMenuModerador;
import grafica.moderador.controladores.ControladorVentanaNuevaPartida;
import javax.swing.JTextPane;
import java.awt.*;
import java.io.Serializable;
import java.util.HashMap;
import javax.swing.*;
public class VentanaNuevaPartida implements Serializable{

	private JDialog jDialog = null;  
	private JPanel jContentPane = null;
	private JPanel jPanelCodigo = null;
	private JPanel jPanelInfo = null;
	private JLabel jLabelCodigo = null;
	private JTextField jTextFieldCodigo = null;
	private JPanel jPanelJugadores = null;
	private JPanel jPanelBotones = null;
	private JPanel jPanelIngreso = null;
	private JLabel jLabelJugadores = null;
	private JButton jButtonAceptar = null;
	private JButton jButtonCancelar = null;
	private JPanel jPanelIngresoJugadores = null;
	private JPanel jPanelJugador1 = null;
	private JPanel jPanelJugador2 = null;
	private JPanel jPanelJugador3 = null;
	private JPanel jPanelJugador4 = null;
	private JPanel jPanelJugador5 = null;
	private JPanel jPanelJugador6 = null;
	private JLabel jLabelJugador1 = null;
	private JTextField jTextFieldJugador1 = null;
	private JComboBox jComboBoxJugador1 = null;
	private JLabel jLabelJugador2 = null;
	private JTextField jTextFieldJugador2 = null;
	private JComboBox jComboBoxJugador2 = null;
	private JLabel jLabelJugador3 = null;
	private JTextField jTextFieldJugador3 = null;
	private JComboBox jComboBoxJugador3 = null;
	private JLabel jLabelJugador4 = null;
	private JTextField jTextFieldJugador4 = null;
	private JComboBox jComboBoxJugador4 = null;
	private JLabel jLabelJugador5 = null;
	private JTextField jTextFieldJugador5 = null;
	private JComboBox jComboBoxJugador5 = null;
	private JLabel jLabelJugador6 = null;
	private JTextField jTextFieldJugador6 = null;
	private JComboBox jComboBoxJugador6 = null;
	private ControladorVentanaNuevaPartida controladorVentanaNuevaPartida;
	private JTextPane jTextPane = null;
	HashMap<String, Color> colors;   

	private final static long serialVersionUID = 1;
	
	public VentanaNuevaPartida(ControladorMenuModerador controladorMenuModerador){
		controladorVentanaNuevaPartida = new ControladorVentanaNuevaPartida(this,controladorMenuModerador);
		colors = new HashMap<String, Color>();
	    colors.put("WHITE", Color.WHITE);
	    colors.put("RED", Color.RED);
	    colors.put("GREEN", Color.GREEN);
	    colors.put("YELLOW", Color.YELLOW);
	    colors.put("BLUE", Color.BLUE);
	    colors.put("ORANGE", Color.ORANGE);
	}
	
	private JDialog getJDialog() {
		if (jDialog == null) {
			jDialog = new JDialog();
			jDialog.setSize(new java.awt.Dimension(335,500));
			jDialog.setTitle("Nueva Partida");
			jDialog.setContentPane(getJContentPane());
			jDialog.setModal(true); 
			jDialog.setResizable (false);
			jDialog.setLocationRelativeTo(null);

		}
		return jDialog;
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelCodigo(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getJPanelInfo(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getJPanelJugadores(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}

	private JPanel getJPanelCodigo() {
		if (jPanelCodigo == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(20);
			flowLayout.setAlignment(java.awt.FlowLayout.CENTER);
			flowLayout.setVgap(20);
			jLabelCodigo = new JLabel();
			jLabelCodigo.setText("Codigo");
			jPanelCodigo = new JPanel();
			jPanelCodigo.setLayout(flowLayout);
			jPanelCodigo.setPreferredSize(new java.awt.Dimension(10,70));
			jPanelCodigo.add(jLabelCodigo, null);
			jPanelCodigo.add(getJTextFieldCodigo(), null);
		}
		return jPanelCodigo;
	}

	private JPanel getJPanelInfo() {
		if (jPanelInfo == null) {
			jPanelInfo = new JPanel();
			jPanelInfo.setPreferredSize(new java.awt.Dimension(10,40));
			jPanelInfo.add(getJTextPane(), null);
		}
		return jPanelInfo;
	}

	private JTextField getJTextFieldCodigo() {
		if (jTextFieldCodigo == null) {
			jTextFieldCodigo = new JTextField();
			jTextFieldCodigo.setPreferredSize(new java.awt.Dimension(100,20));
		}
		return jTextFieldCodigo;
	}

	private JPanel getJPanelJugadores() {
		if (jPanelJugadores == null) {
			jPanelJugadores = new JPanel();
			jPanelJugadores.setLayout(new BorderLayout());
			jPanelJugadores.add(getJPanelBotones(), java.awt.BorderLayout.SOUTH);
			jPanelJugadores.add(getJPanelIngreso(), java.awt.BorderLayout.NORTH);
			jPanelJugadores.add(getJPanelIngresoJugadores(), java.awt.BorderLayout.CENTER);
		}
		return jPanelJugadores;
	}

	private JPanel getJPanelBotones() {
		if (jPanelBotones == null) {
			FlowLayout flowLayout1 = new FlowLayout();
			flowLayout1.setHgap(30);
			flowLayout1.setVgap(8);
			jPanelBotones = new JPanel();
			jPanelBotones.setLayout(flowLayout1);
			jPanelBotones.setPreferredSize(new java.awt.Dimension(10,50));
			jPanelBotones.add(getJButtonAceptar(), null);
			jPanelBotones.add(getJButtonCancelar(), null);
		}
		return jPanelBotones;
	}

	private JPanel getJPanelIngreso() {
		if (jPanelIngreso == null) {
			jLabelJugadores = new JLabel();
			jLabelJugadores.setText("Jugadores");
			jPanelIngreso = new JPanel();
			jPanelIngreso.setPreferredSize(new java.awt.Dimension(10,30));
			jPanelIngreso.add(jLabelJugadores, null);
		}
		return jPanelIngreso;
	}

	private JButton getJButtonAceptar() {
		if (jButtonAceptar == null) {
			jButtonAceptar = new JButton("Aceptar");
			jButtonAceptar.setPreferredSize(new java.awt.Dimension(90,30));
			ActionListener manBoton = new ActionListener(){
				public void actionPerformed (ActionEvent e){
					controladorVentanaNuevaPartida.crearNuevaPartida();
					//jDialog.dispose();
				}
			};
			jButtonAceptar.addActionListener(manBoton);
		}
		return jButtonAceptar;
	}
	

	private JButton getJButtonCancelar() {
		if (jButtonCancelar == null) {
			jButtonCancelar = new JButton("Cancelar");
			jButtonCancelar.setPreferredSize(new java.awt.Dimension(90,30));
			ActionListener manBoton = new ActionListener(){
				public void actionPerformed (ActionEvent e){
					jDialog.dispose();
				}
			};
			jButtonCancelar.addActionListener(manBoton);
		}
		return jButtonCancelar;
	}

	private JPanel getJPanelIngresoJugadores() {
		if (jPanelIngresoJugadores == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(6);
			gridLayout.setColumns(1);
			jPanelIngresoJugadores = new JPanel();
			jPanelIngresoJugadores.setLayout(gridLayout);
			jPanelIngresoJugadores.add(getJPanelJugador1(), null);
			jPanelIngresoJugadores.add(getJPanelJugador2(), null);
			jPanelIngresoJugadores.add(getJPanelJugador3(), null);
			jPanelIngresoJugadores.add(getJPanelJugador4(), null);
			jPanelIngresoJugadores.add(getJPanelJugador5(), null);
			jPanelIngresoJugadores.add(getJPanelJugador6(), null);
		}
		return jPanelIngresoJugadores;
	}

	private JPanel getJPanelJugador1() {
		if (jPanelJugador1 == null) {
			FlowLayout flowLayout2 = new FlowLayout();
			flowLayout2.setHgap(15);
			flowLayout2.setVgap(13);
			jLabelJugador1 = new JLabel();
			jLabelJugador1.setText("Jugador1");
			jPanelJugador1 = new JPanel();
			jPanelJugador1.setLayout(flowLayout2);
			jPanelJugador1.add(jLabelJugador1, null);
			jPanelJugador1.add(getJTextFieldJugador1(), null);
			jPanelJugador1.add(getJComboBoxJugador1(), null);
		}
		return jPanelJugador1;
	}

	private JPanel getJPanelJugador2() {
		if (jPanelJugador2 == null) {
			jLabelJugador2 = new JLabel();
			jLabelJugador2.setText("Jugador 2");
			FlowLayout flowLayout3 = new FlowLayout();
			flowLayout3.setHgap(15);
			flowLayout3.setVgap(13);
			jPanelJugador2 = new JPanel();
			jPanelJugador2.setLayout(flowLayout3);
			jPanelJugador2.add(jLabelJugador2, null);
			jPanelJugador2.add(getJTextFieldJugador2(), null);
			jPanelJugador2.add(getJComboBoxJugador2(), null);
		}
		return jPanelJugador2;
	}

	private JPanel getJPanelJugador3() {
		if (jPanelJugador3 == null) {
			jLabelJugador3 = new JLabel();
			jLabelJugador3.setText("Jugador 3");
			FlowLayout flowLayout4 = new FlowLayout();
			flowLayout4.setHgap(15);
			flowLayout4.setVgap(13);
			jPanelJugador3 = new JPanel();
			jPanelJugador3.setLayout(flowLayout4);
			jPanelJugador3.add(jLabelJugador3, null);
			jPanelJugador3.add(getJTextFieldJugador3(), null);
			jPanelJugador3.add(getJComboBoxJugador3(), null);
		}
		return jPanelJugador3;
	}

	private JPanel getJPanelJugador4() {
		if (jPanelJugador4 == null) {
			jLabelJugador4 = new JLabel();
			jLabelJugador4.setText("Jugador 4");
			FlowLayout flowLayout5 = new FlowLayout();
			flowLayout5.setHgap(15);
			flowLayout5.setVgap(13);
			jPanelJugador4 = new JPanel();
			jPanelJugador4.setLayout(flowLayout5);
			jPanelJugador4.add(jLabelJugador4, null);
			jPanelJugador4.add(getJTextFieldJugador4(), null);
			jPanelJugador4.add(getJComboBoxJugador4(), null);
		}
		return jPanelJugador4;
	}

	private JPanel getJPanelJugador5() {
		if (jPanelJugador5 == null) {
			jLabelJugador5 = new JLabel();
			jLabelJugador5.setText("Jugador 5");
			FlowLayout flowLayout6 = new FlowLayout();
			flowLayout6.setHgap(15);
			flowLayout6.setVgap(13);
			jPanelJugador5 = new JPanel();
			jPanelJugador5.setLayout(flowLayout6);
			jPanelJugador5.add(jLabelJugador5, null);
			jPanelJugador5.add(getJTextFieldJugador5(), null);
			jPanelJugador5.add(getJComboBoxJugador5(), null);
		}
		return jPanelJugador5;
	}

	private JPanel getJPanelJugador6() {
		if (jPanelJugador6 == null) {
			jLabelJugador6 = new JLabel();
			jLabelJugador6.setText("Jugador 6");
			FlowLayout flowLayout7 = new FlowLayout();
			flowLayout7.setHgap(15);
			flowLayout7.setVgap(13);
			jPanelJugador6 = new JPanel();
			jPanelJugador6.setLayout(flowLayout7);
			jPanelJugador6.add(jLabelJugador6, null);
			jPanelJugador6.add(getJTextFieldJugador6(), null);
			jPanelJugador6.add(getJComboBoxJugador6(), null);
		}
		return jPanelJugador6;
	}

	private JTextField getJTextFieldJugador1() {
		if (jTextFieldJugador1 == null) {
			jTextFieldJugador1 = new JTextField();
			jTextFieldJugador1.setPreferredSize(new java.awt.Dimension(100,20));
		}
		return jTextFieldJugador1;
	}
////////////////////////////////////////////////////
	private JComboBox getJComboBoxJugador1() {
		if (jComboBoxJugador1 == null) {

			jComboBoxJugador1 = new JComboBox(colors.keySet().toArray());
			jComboBoxJugador1.setRenderer(myRenderer);
			jComboBoxJugador1.setPreferredSize(new java.awt.Dimension(75,20));
			jComboBoxJugador1.setSelectedIndex(0);
		
		}
		return jComboBoxJugador1;
	}

	private JTextField getJTextFieldJugador2() {
		if (jTextFieldJugador2 == null) {
			jTextFieldJugador2 = new JTextField();
			jTextFieldJugador2.setPreferredSize(new java.awt.Dimension(100,20));
		}
		return jTextFieldJugador2;
	}

	private JComboBox getJComboBoxJugador2() {
		if (jComboBoxJugador2 == null) {

			jComboBoxJugador2 = new JComboBox(colors.keySet().toArray());
			jComboBoxJugador2.setRenderer(myRenderer);
			jComboBoxJugador2.setPreferredSize(new java.awt.Dimension(75,20));
			jComboBoxJugador2.setSelectedIndex(1);
			}
		return jComboBoxJugador2;
	}

	private JTextField getJTextFieldJugador3() {
		if (jTextFieldJugador3 == null) {
			jTextFieldJugador3 = new JTextField();
			jTextFieldJugador3.setPreferredSize(new java.awt.Dimension(100,20));
		
		}
		return jTextFieldJugador3;
	}

	private JComboBox getJComboBoxJugador3() {
		if (jComboBoxJugador3 == null) {

			jComboBoxJugador3 = new JComboBox(colors.keySet().toArray());
			jComboBoxJugador3.setRenderer(myRenderer);
			jComboBoxJugador3.setPreferredSize(new java.awt.Dimension(75,20));
			jComboBoxJugador3.setSelectedIndex(2);
		}
		return jComboBoxJugador3;
	}

	private JTextField getJTextFieldJugador4() {
		if (jTextFieldJugador4 == null) {
			jTextFieldJugador4 = new JTextField();
			jTextFieldJugador4.setPreferredSize(new java.awt.Dimension(100,20));
		}
		return jTextFieldJugador4;
	}

	private JComboBox getJComboBoxJugador4() {
		if (jComboBoxJugador4 == null) {

			jComboBoxJugador4 = new JComboBox(colors.keySet().toArray());
			jComboBoxJugador4.setRenderer(myRenderer);
			jComboBoxJugador4.setPreferredSize(new java.awt.Dimension(75,20));
			jComboBoxJugador4.setSelectedIndex(3);
		}
		return jComboBoxJugador4;
	}

	private JTextField getJTextFieldJugador5() {
		if (jTextFieldJugador5 == null) {

			jTextFieldJugador5 = new JTextField();
			jTextFieldJugador5.setPreferredSize(new java.awt.Dimension(100,20));
		
		}
		return jTextFieldJugador5;
	}

	private JComboBox getJComboBoxJugador5() {
		if (jComboBoxJugador5 == null) {

			jComboBoxJugador5 = new JComboBox(colors.keySet().toArray());
			jComboBoxJugador5.setRenderer(myRenderer);
			jComboBoxJugador5.setPreferredSize(new java.awt.Dimension(75,20));
			jComboBoxJugador5.setSelectedIndex(4);
		}
		return jComboBoxJugador5;
	}

	private JTextField getJTextFieldJugador6() {
		if (jTextFieldJugador6 == null) {
			jTextFieldJugador6 = new JTextField();
			jTextFieldJugador6.setPreferredSize(new java.awt.Dimension(100,20));
		}
		return jTextFieldJugador6;
	}

	private JComboBox getJComboBoxJugador6() {
		if (jComboBoxJugador6 == null) {

			jComboBoxJugador6 = new JComboBox(colors.keySet().toArray());
			jComboBoxJugador6.setRenderer(myRenderer);
			jComboBoxJugador6.setPreferredSize(new java.awt.Dimension(75,20));
			jComboBoxJugador6.setSelectedIndex(5);
		}
		return jComboBoxJugador6;
	}
	
	public void setVisible (boolean b)	{	
		this.getJDialog().setVisible(b);
	}
	
	public void setJTextPane(String info){
		jTextPane.setText(info);
	}
	
	public String getJugador1(){
		return jTextFieldJugador1.getText();
	}
	public String getJugador2(){
		return jTextFieldJugador2.getText();
	}
	public String getJugador3(){
		return jTextFieldJugador3.getText();
	}
	public String getJugador4(){
		return jTextFieldJugador4.getText();
	}
	public String getJugador5(){
		return jTextFieldJugador5.getText();
	}
	public String getJugador6(){
		return jTextFieldJugador6.getText();
	}
	public String getCodigo(){
		return jTextFieldCodigo.getText();
	}
	
	public void cerrar(){
		jDialog.dispose();;
	}

	private JTextPane getJTextPane() {
		if (jTextPane == null) {
			jTextPane = new JTextPane();
			jTextPane.setFont(new Font("Dialog",Font.ITALIC,10));
			setJTextPane("Info:");
			jTextPane.setPreferredSize(new java.awt.Dimension(270,30));
		}
		return jTextPane;
	}
	
		
    private DefaultListCellRenderer myRenderer = new DefaultListCellRenderer() {
        
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            String val = value.toString();
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
            	HashMap<String, Color> colors = new HashMap<String, Color>();   
                colors.put("WHITE", Color.WHITE);
                colors.put("RED", Color.RED);
                colors.put("GREEN", Color.GREEN);
                colors.put("YELLOW", Color.YELLOW);
                colors.put("BLUE", Color.BLUE);
                colors.put("ORANGE", Color.ORANGE);
                setBackground(colors.get(val.toUpperCase()));
            }
            return this;
        }
    };  
	        
    public String getColJComboBoxJugador1(){
    	return (String)jComboBoxJugador1.getSelectedItem();
    }
    public String getColJComboBoxJugador2(){
    	return (String)jComboBoxJugador2.getSelectedItem();
    }
    public String getColJComboBoxJugador3(){
    	return (String)jComboBoxJugador3.getSelectedItem();
    }
    public String getColJComboBoxJugador4(){
    	return (String)jComboBoxJugador4.getSelectedItem();
    }
    public String getColJComboBoxJugador5(){
    	return (String)jComboBoxJugador5.getSelectedItem();
    }
    public String getColJComboBoxJugador6(){
    	return (String)jComboBoxJugador6.getSelectedItem();
    }
    
    public Color Color(String colStr){
    	Color color =null;
    	if(colStr.equals("WHITE"))
    		color = Color.WHITE;
		else
			if(colStr.equals("RED"))
				color = Color.RED;	
	    		else
	    			if(colStr.equals("GREEN"))
	    				color = Color.GREEN;	
	    	    		else
	    	    			if(colStr.equals("YELLOW"))
	    	    				color = Color.YELLOW;	
	    	    	    		else
	    	    	    			if(colStr.equals("BLUE"))
	    	    	    				color = Color.BLUE;	
    	    	    	    		else
    	    	    	    			if(colStr.equals("ORANGE"))
    	    	    	    				color = Color.ORANGE;	
    	
    	return color;
    }


	
}
